package com.ef.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.StatusType;
import com.ef.dao.ClassTypeDao;
import com.ef.domain.ClassType;
import com.ef.service.ClassTypeService;
import com.ef.util.CustomExample;
import com.ef.util.PaginationSupport;

public class ClassTypeServiceImpl extends BaseServiceImpl implements
		ClassTypeService {

	private ClassTypeDao classTypeDao;

	public PaginationSupport<ClassType> findByExample(
			CustomExample<ClassType> example, int startIndex, int pageSize) {

		return classTypeDao.findPageByExample(example, startIndex, pageSize);
	}

	public void saveOrUpdate(ClassType entity) {
		classTypeDao.saveOrUpdate(entity);
	}

	public void delete(ClassType entity) {
		entity.setStatus(StatusType.INVALID);
		classTypeDao.update(entity);
	}

	public List<ClassType> findAll() {
		DetachedCriteria criteria = DetachedCriteria.forClass(ClassType.class);
		criteria.add(Restrictions.eq("status", StatusType.VALID));
		criteria.addOrder(Order.asc("description"));
		return classTypeDao.findAllByCriteria(criteria);

	}

	public List<ClassType> findALLValid() {
		DetachedCriteria criterion = DetachedCriteria.forClass(ClassType.class);
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		criterion.addOrder(Order.asc("description"));
		return classTypeDao.findAllByCriteria(criterion);
	}

	public ClassType findById(Long id) {
		return classTypeDao.findById(id);
	}

	public ClassTypeDao getClassTypeDao() {
		return classTypeDao;
	}

	public void setClassTypeDao(ClassTypeDao classTypeDao) {
		this.classTypeDao = classTypeDao;
	}

	public List<ClassType> findByName(String name, int max) {
		DetachedCriteria criterion = DetachedCriteria.forClass(ClassType.class);
		criterion.add(Restrictions.like("name", name
				.trim(), MatchMode.ANYWHERE));
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		criterion.addOrder(Order.asc("description"));
		return classTypeDao.findAllByCriteria(criterion, max);
	}

	public List<ClassType> findByName(String name) {
		DetachedCriteria criterion = DetachedCriteria.forClass(ClassType.class);
		criterion.add(Restrictions.eq("name", name));
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		criterion.addOrder(Order.asc("description"));
		return classTypeDao.findAllByCriteria(criterion);
	}

}
